﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dados.DSMPTUDOTableAdapters;
using System.Data.SqlClient;
using System.Data;

namespace Dados
{
    public class VideoDAL
    {
         static T_VIDEOTableAdapter videoTA = new T_VIDEOTableAdapter();

       public static DSMPTUDO.T_VIDEODataTable ConsultaTodos()
       {
           try
           {
               return videoTA.GetData();
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message);
           }
       }
       public static DSMPTUDO.T_VIDEODataTable ConsultaPorCodigo(int codigo)
       {
           try
           {
               return videoTA.GetVideoPorCodigo(codigo);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message);
           }
       }

       public static DSMPTUDO.T_VIDEODataTable ConsultaPorParametro(string nome, int genero, int autor, int formato, int especificacao)
       {
           if (nome == null) nome = "";
           string video = string.Format("Select * from T_VIDEO where tx_nome like '%" + nome.ToString() + "%'" +
               (genero != 0 ? " AND nr_genero=" + genero.ToString() : "") +
               (autor != 0 ? " AND nr_autor=" + autor.ToString() : "") +
               (formato != 0 ? " AND nr_formato=" + formato.ToString() : "") +
               (especificacao != 0 ? " AND nr_especificacao=" + especificacao.ToString() : ""));

           SqlConnection conn = new SqlConnection(videoTA.Connection.ConnectionString);
           SqlCommand cmd = new SqlCommand(video, conn);
           cmd.CommandType = CommandType.Text;

           videoTA.Adapter.SelectCommand = cmd;
           DSMPTUDO.T_VIDEODataTable table = new DSMPTUDO.T_VIDEODataTable();
           videoTA.Adapter.Fill(table);
           return table;
       }

       public static bool Insere(string nome, string localizacao, int genero, int autor, int formato, int especificacao, int software, string usuario )
       {
           try
           {
               return ((videoTA.Insert(nome, localizacao, genero, autor, formato, especificacao, software, usuario) == 1));
                   
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message);
           }
       }
       public static bool Atualiza(int codigo, string nome, string localizacao, int genero, int autor, int formato, int especificacao, int software, string usuario)
       {
           try
           {
               return (videoTA.Update(nome, localizacao, genero, autor, formato, especificacao, software, usuario, codigo) == 1);
             
                   
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message);
           }
       }
      public static bool Remove(int codigo)
      {
          try
          {
              return (videoTA.Delete(codigo) == 1);
          }
          catch (Exception ex)
          {
              throw new Exception(ex.Message);
          }
      }
        public static DSMPTUDO.T_VIDEODataTable ConsultaPorUsuario(string usuario){
            try
            {

                return (videoTA.GetVideosPorUsuario(usuario));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        
        }
    }
}